package com.bocai.utils;

import java.text.DecimalFormat;
import java.util.Random;

public class NumberUtils {
	/**
	 * 生成数字随机数
	 * 
	 * @param length
	 *            需要生成随机数的长度
	 * @return 生成的随机数
	 */
	private static final String[] chars = { "0", "1", "2", "3", "4", "5", "6",
		"7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
		"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
		"X", "Y", "Z" };
	public static String generatorNumber(int length) {
		StringBuffer sb = new StringBuffer();
		Random r = new Random();
		for (int i = 0; i < length; i++) {
			sb.append(chars[r.nextInt(10)]);
		}
		return sb.toString();
	}
	/**
	 * 随机生成含数字大小写的字符串
	 * @param length
	 * @return
	 */
	public static String getRandomCharAndNumr(Integer length) {  
	    String str = "";  
	    Random random = new Random();  
	    for (int i = 0; i < length; i++) {  
	        boolean b = random.nextBoolean(); 
	        
	        if (b) { // 字符串  
	            b = random.nextBoolean(); 
	            str += b? (char) (65 + random.nextInt(26)): (char) (97 + random.nextInt(26));
	        } else { // 数字  
	            str += String.valueOf(random.nextInt(10));  
	        }  
	    }  
	    return str;  
	}  
	/**
	 * 保留两位小数
	 * @param val
	 * @return
	 */
	public static String getDoubleFormat(double val){
		DecimalFormat df=new DecimalFormat(".##");
		return df.format(val);
	}
}
